//
//  UINavigationController+PoP.m
//  高仿暴走斗图
//
//  Created by key on 16/3/21.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UINavigationController+PoP.h"

@implementation UINavigationController (PoP)

- (void)customPoPViewControllerWithTransition:(kHSYCustomViewAnimationTransitionType)transition subtype:(NSString *)subtype completion:(void (^)())completion {
    
    if (self.viewControllers.count <= 1) {
        return;
    }
    @weakify(self);
    switch (transition) {
        case kHSYCustomViewAnimationTransitionTypeNone:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionNone
                                     changesCompletion:^{
                                         @strongify(self);
                                         [self popViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFlipFromLeft:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromLeft
                                     changesCompletion:^{
                                         @strongify(self);
                                         [self popViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFlipFromRight:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromRight
                                     changesCompletion:^{
                                         @strongify(self);
                                         [self popViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeCurlUp:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlUp
                                     changesCompletion:^{
                                         @strongify(self);
                                         [self popViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeCurlDown:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlDown
                                     changesCompletion:^{
                                         @strongify(self);
                                         [self popViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFadeIn:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionFade subtype:nil
                                 changesCompletion:^{
                                     @strongify(self);
                                     [self popViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeMoveIn:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionMoveIn subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     [self popViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypePush:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionPush subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     [self popViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeReveal:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionReveal subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     [self popViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        default:
            break;
    }
    
    completion();
}



- (NSArray *)customPoPToRootViewControllerWithTransition:(kHSYCustomViewAnimationTransitionType)transition subtype:(NSString *)subtype completion:(void (^)())completion {
    
    static NSArray *popViewController = nil;
    @weakify(self);
    switch (transition) {
        case kHSYCustomViewAnimationTransitionTypeNone:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionNone
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToRootViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFlipFromLeft:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromLeft
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToRootViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFlipFromRight:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromRight
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToRootViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeCurlUp:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlUp
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToRootViewControllerAnimated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeCurlDown:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlDown
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToRootViewControllerAnimated:NO];
                                     }];
        }
            break;
    
        case kHSYCustomViewAnimationTransitionTypeFadeIn:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionFade subtype:nil
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToRootViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeMoveIn:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionMoveIn subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToRootViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypePush:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionPush subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToRootViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeReveal:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionReveal subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToRootViewControllerAnimated:NO];
                                 }];
        }
            break;
            
        default:
            break;
    }
    
    completion();
    
    return popViewController;
}


- (NSArray *)customPoPToViewController:(UIViewController *)viewController withCustomTransition:(kHSYCustomViewAnimationTransitionType)transition subtype:(NSString *)subtype completion:(void (^)())completion {
    
    static NSArray *popViewController = nil;
    @weakify(self);
    switch (transition) {
        case kHSYCustomViewAnimationTransitionTypeNone:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionNone
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToViewController:viewController animated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFlipFromLeft:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromLeft
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToViewController:viewController animated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFlipFromRight:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromRight
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToViewController:viewController animated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeCurlUp:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlUp
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToViewController:viewController animated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeCurlDown:{
            [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlDown
                                     changesCompletion:^{
                                         @strongify(self);
                                         popViewController = [self popToViewController:viewController animated:NO];
                                     }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeFadeIn:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionFade subtype:nil
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToViewController:viewController animated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeMoveIn:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionMoveIn subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToViewController:viewController animated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypePush:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionPush subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToViewController:viewController animated:NO];
                                 }];
        }
            break;
            
        case kHSYCustomViewAnimationTransitionTypeReveal:{
            [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionReveal subtype:subtype
                                 changesCompletion:^{
                                     @strongify(self);
                                     popViewController = [self popToViewController:viewController animated:NO];
                                 }];
        }
            break;
        default:
            break;
    }
    
    completion();
    
    return popViewController;
}


- (RACSignal *)customPoPToViewController:(UIViewController *)viewController withCustomTransition:(kHSYCustomViewAnimationTransitionType)transition subtype:(NSString *)subtype {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        if (self.viewControllers.count <= 1) {
            return nil;
        }
        @weakify(self);
        switch (transition) {
            case kHSYCustomViewAnimationTransitionTypeNone:{
                [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionNone
                                    changesCompletion:^{
                                        @strongify(self);
                                        [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                        [subscriber sendCompleted];
                                    }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeFlipFromLeft:{
                [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromLeft
                                    changesCompletion:^{
                                        @strongify(self);
                                        [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                        [subscriber sendCompleted];
                                    }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeFlipFromRight:{
                [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionFlipFromRight
                                    changesCompletion:^{
                                        @strongify(self);
                                        [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                        [subscriber sendCompleted];
                                    }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeCurlUp:{
                [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlUp
                                    changesCompletion:^{
                                        @strongify(self);
                                        [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                        [subscriber sendCompleted];
                                    }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeCurlDown:{
                [self standardAnimationWithController:nil duration:[self animationDuration] options:UIViewAnimationOptionTransitionCurlDown
                                    changesCompletion:^{
                                        @strongify(self);
                                        [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                        [subscriber sendCompleted];
                                    }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeFadeIn:{
                [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionFade subtype:nil
                                changesCompletion:^{
                                    @strongify(self);
                                    [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                    [subscriber sendCompleted];
                                }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeMoveIn:{
                [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionMoveIn subtype:subtype
                                changesCompletion:^{
                                    @strongify(self);
                                    [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                    [subscriber sendCompleted];
                                }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypePush:{
                [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionPush subtype:subtype
                                changesCompletion:^{
                                    @strongify(self);
                                    [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                    [subscriber sendCompleted];
                                }];
            }
                break;
                
            case kHSYCustomViewAnimationTransitionTypeReveal:{
                [self coreAnimationWithController:nil duration:[self animationDuration] type:kCATransitionReveal subtype:subtype
                                changesCompletion:^{
                                    @strongify(self);
                                    [subscriber sendNext:[self popToViewController:viewController animated:NO]];
                                    [subscriber sendCompleted];
                                }];
            }
                break;
            default:
                break;
        }

        return nil;
    }];
}


@end
